﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib;

namespace DVSProject.Controllers
{
    public class ReportVisitController : Controller
    {
        //
        // GET: /ReportVisit/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportVisitModels ReportModel = new ReportVisitModels();
            ReportModel.FSRNOText = "";
            return View();
        }
        public ActionResult IndexView()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportVisitModels _ReportvisitformModel = new ReportVisitModels();
            _ReportvisitformModel.FSRNOText = "";


            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            _ReportvisitformModel.LocationSelects = selectlist;
            //ReportModel.start_date = DateTime.Now;
            //ReportModel.end_date = DateTime.Now;

            //ReportRepo _ReportRepo = new ReportRepo();
            //IEnumerable<ProductTypeModels> _ProductTypes = _ReportRepo.GetProductType();
            //string selectedId = "เครื่องล้างจาน";
            //IEnumerable<SelectListItem> selectlist = from s in _ProductTypes
            //                                         select new SelectListItem
            //                                         {
            //                                             Selected = (s.NameThValue == selectedId),
            //                                             Text = s.NameThText,
            //                                             Value = s.NameThValue.ToString()
            //                                         };

            ////SF  _SF = new SF();


            //IEnumerable<MonthModels> _Months = _ReportRepo.GetMonthAll();
            //string selectedMachinId = "มกราคม";
            //IEnumerable<SelectListItem> MonthShowSelect = from s in _Months
            //                                              select new SelectListItem
            //                                              {
            //                                                  Selected = (s.month_name == selectedMachinId),
            //                                                  Text = s.month_name,
            //                                                  Value = s.month_name.ToString()
            //                                              };

            //// ReportModel.ProductSelect = selectlist;
            //ReportModel.MonthShowSelect = MonthShowSelect;
           
            return View(_ReportvisitformModel);
        }
         [HttpPost]
        public ActionResult IndexView(FormCollection collection)
        {
            string locationSetCode="";
            string _start_date = "";
            string _end_date = "";
            string _FSRNOText = "";
            string createBy = "";

            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportVisitModels _ReportvisitformModel = new ReportVisitModels();
            

            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            _ReportvisitformModel.LocationSelects = selectlist;


            //if ((Convert.ToBoolean(collection["CheckLocation"].Split(',')[0])) == true)
            //{
                 
            //    // this.HttpContext.Session["locationSetCode"] = locationSetCode.Trim();
            //}

            //if ((Convert.ToBoolean(collection["CheckVisitDate"].Split(',')[0])) == true)
            //{
                   

            //     //  this.HttpContext.Session["_start_date"] =_start_date;
            //     //  this.HttpContext.Session["_end_date"] = _end_date;
            //}
            //if ((Convert.ToBoolean(collection["CheckFSRNo"].Split(',')[0])) == true)
            //{
                
            //  //   this.HttpContext.Session["FsrNoText"] = _FSRNOText.Trim();
            //}

            locationSetCode = collection["locationSetCode"];
            _start_date = collection["start_date"];
            _end_date = collection["end_date"];
            _FSRNOText = collection["FSRNOText"];

            ReportRepo report = new ReportRepo();
            DVSProject.Models.EmployeeModels employee = (DVSProject.Models.EmployeeModels)Session["Employee"];
            if (employee.dept_id == 1)
            {
                createBy = (string)Session["User"];
            }

            _ReportvisitformModel.AccessList = report.GetVisitReport(locationSetCode, _start_date, _end_date, _FSRNOText, createBy);



            return View(_ReportvisitformModel);
            
           // this.HttpContext.Session["ReportName"] = "rptEVisitForm.rpt";
            

           // Response.Redirect("~/AspNetForms/aspnetvisitform.aspx");

        }
        
        public void ViewReport(string FSRNOText)
        {

            string _FSRNOText = FSRNOText;

            this.HttpContext.Session["ReportName"] = "rptEVisitForm.rpt";
            this.HttpContext.Session["FsrNoText"] = _FSRNOText.Trim();
         

            Response.Redirect("~/AspNetForms/aspnetvisitform.aspx");
             
        }


    }
}
